package cc.vv.btpartyjob.partymanager.fragment

import android.content.Intent
import android.os.Bundle
import android.text.TextUtils
import cc.vv.baselibrary.util.UserInfoManageUtil
import cc.vv.btpartyjob.jnimodule.enums.H5UrlEnum
import cc.vv.btpartyjob.jnimodule.enums.RoleType
import cc.vv.btpartyjob.jnimodule.globle.IntentJniKey
import cc.vv.btpartyjob.jnimodule.server.JniStartActivity
import cc.vv.btpartyjob.jnimodule.server.UserRoleServer
import cc.vv.btpartyjob.partymanager.activity.*
import cc.vv.btpartyjob.partymanager.delegate.PartyManagerMainFragmentDelegate
import cc.vv.btpartyjob.partymanager.inter.OperationItemClickInterface
import cc.vv.btpartyjob.partymanager.moudel.bean.OperationBean
import cc.vv.btpartyjob.partymanager.server.PartyManagerMainFragmentServer
import cc.vv.mvp.binder.BaseDataBinder
import cc.vv.mvp.fragment.BaseFragmentMVP

/**
 * 党务管理Fragment界面
 * Created by Chen.qi on 2018/10/24.
 */
class PartyManagerMainFragment : BaseFragmentMVP<PartyManagerMainFragmentDelegate>(), OperationItemClickInterface {

    override fun initView(bundle: Bundle?) {
        super.initView(bundle)
        viewDelegate?.setSheQuDangWeiData(PartyManagerMainFragmentServer.getSheQuDangWei(), this)
        //四双
        viewDelegate?.setDoubleServerAndData(PartyManagerMainFragmentServer.getSiShuangDataList(), this)
        //街道联席会议
        viewDelegate?.setStreetMeetingData(PartyManagerMainFragmentServer.getMeeting(), this)

        if (UserRoleServer.getOnlyPartyRole()) {
            viewDelegate?.isShowProjectManager(false)
        } else {
            if (UserRoleServer.getAreaRole() || UserRoleServer.getStreetRole()
                    || UserRoleServer.getSheQuRole() || UserRoleServer.getGridRole()) {
                viewDelegate?.isShowProjectManager(true)
                //项目管理
                viewDelegate?.setProjectManagerData(PartyManagerMainFragmentServer.getProjectManager(),
                        this)
            }
        }

        //非公党建
        viewDelegate?.setNoPublicPartyData(PartyManagerMainFragmentServer.getNoPublicParty(), this)
        if (UserRoleServer.getFourTeamsRole() || UserRoleServer.getAreaRole()) {
            viewDelegate?.isShowTuoPingGongJian(true)
            //脱贫攻坚
            viewDelegate?.setPovertyAlleviationData(PartyManagerMainFragmentServer.getPovertyAlleviation(), this)
        } else {
            viewDelegate?.isShowTuoPingGongJian(false)
        }

        if (UserRoleServer.getAreaRole()) {
            viewDelegate?.isShowDataSupport(true)
            //数据支撑
            viewDelegate?.setDataSupportData(PartyManagerMainFragmentServer.getDataSupport(), this)
        } else {
            viewDelegate?.isShowDataSupport(false)
        }

    }

    override fun initData() {
    }

    override fun bindEvenListener() {
    }

    override fun getDelegateClass(): Class<PartyManagerMainFragmentDelegate> {
        return PartyManagerMainFragmentDelegate::class.java
    }

    override fun getDataBinder(): BaseDataBinder<*, *>? {
        return null
    }

    override fun clickItem(operaObj: OperationBean) {
        when {
            "社区组织" == operaObj.title -> {
                val intent = Intent(activity, SheQuPartyDetailActivity::class.java)
                startActivity(intent)

            }
            "网格" == operaObj.title -> {
                val intent = Intent(activity, WangGeListActivity::class.java)
                intent.putExtra("isShow", true)
                startActivity(intent)
            }
            "楼栋党小组" == operaObj.title -> {
                val intent = Intent(activity, LouDongTeamActivity::class.java)
                startActivity(intent)
            }
            "单元中心户" == operaObj.title -> {
                val intent = Intent(activity, UnitHouseDetailActivity::class.java)
                startActivity(intent)
            }
            "社区活动" == operaObj.title -> {
                val intent = Intent(activity, CommunityActivity::class.java)
                startActivity(intent)
            }
            "组织查询" == operaObj.title -> {
                //社区大党委组织查询
                val intent = Intent(activity, ZuZHiInfoDetailActivity::class.java)
                startActivity(intent)
            }
            "单位报到" == operaObj.title -> {
                val userInfo = UserInfoManageUtil.getUserInfo()
                var communityId = ""
                if (TextUtils.isEmpty(communityId)) {
                    val userOrgList = userInfo?.userOrgList
                    if (null != userOrgList) {
                        for (i in userOrgList.indices) {
                            if (userOrgList[i].type == 4) {
                                communityId = userOrgList[i].id
                                break
                            }
                        }
                    }
                }

                val userRole = userInfo?.userRole
                if (null != userRole && userRole.size > 0) {
                    for (i in 0 until userRole.size) {
                        if (RoleType.UNIT_ADMIN.type == userRole[i].type) {
                            val intent = Intent(activity, UnitReportDetailActivity::class.java)
                            intent.putExtra("unitId", communityId)
                            startActivity(intent)
                            break
                        } else if (RoleType.AREA_ADMIN.type == userRole[i].type
                                || RoleType.STREET_ADMIN.type == userRole[i].type
                                || RoleType.SECTION_ADMIN.type == userRole[i].type
                                || RoleType.GRID_ADMIN.type == userRole[i].type) {
                            val intent = Intent(activity, UnitReportActivity::class.java)
                            startActivity(intent)
                            break

                        }
                    }
                }

            }
            "党员报到" == operaObj.title -> {
                if (UserRoleServer.getAreaRole() || UserRoleServer.getStreetRole()
                        || UserRoleServer.getSheQuRole()) {
                    val intent = Intent(activity, PartyRegisterActivity::class.java)
                    startActivity(intent)
                } else {
                    //只是党员
                    val intent = Intent(activity, PartyRegisterDetailActivity::class.java)
                    intent.putExtra("id", UserInfoManageUtil.getUserId())
                    startActivity(intent)
                }
            }
            "报到统计" == operaObj.title -> {
                val intent = Intent(activity, RegisterStatisticsActivity::class.java)
                startActivity(intent)
            }
            "党员承诺" == operaObj.title -> {
//                if (UserRoleServer.getAreaRole() || UserRoleServer.getStreetRole()
//                        || UserRoleServer.getSheQuRole() || UserRoleServer.getGridRole()) {
//                    val intent = Intent(activity, PartyPromisePingjiaListActivity::class.java)
//                    intent.putExtra("id", UserInfoManageUtil.getUserId())
//                    intent.putExtra("title", "党员承诺")
//                    intent.putExtra("type", 0)
//                    startActivity(intent)
//                } else {
//                    val intent = Intent(activity, PartyPromiseActivity::class.java)
//                    startActivity(intent)
//                }
                val intent = Intent(activity, PartyPromiseActivity::class.java)
                startActivity(intent)
            }

            "单位承诺" == operaObj.title -> {
//                if (UserRoleServer.getAreaRole() || UserRoleServer.getStreetRole()
//                        || UserRoleServer.getSheQuRole() || UserRoleServer.getGridRole()) {
//                    val intent = Intent(activity, PartyPromisePingjiaListActivity::class.java)
//                    intent.putExtra("id", UserInfoManageUtil.getUserId())
//                    intent.putExtra("title", "单位承诺")
//                    intent.putExtra("type", 1)
//                    startActivity(intent)
//                } else {
//                    val intent = Intent(activity, UnitPromiseActivity::class.java)
//                    startActivity(intent)
//                }

                val intent = Intent(activity, UnitPromiseActivity::class.java)
                startActivity(intent)

            }
            "活动" == operaObj.title -> {
                val intent = Intent(activity, ActivitiesListActivity::class.java)
                startActivity(intent)
            }
            "简介" == operaObj.title -> {
                val intent = Intent(activity, StreetMeetingActivity::class.java)
                startActivity(intent)
            }
            "项目查询" == operaObj.title -> {
                val intent = Intent(activity, ProjectQueryActivity::class.java)
                startActivity(intent)
            }
            "添加项目" == operaObj.title -> {
                val intent = Intent(activity, AddProjectActivity::class.java)
                startActivity(intent)
            }
            "项目预警" == operaObj.title -> {
                val intent = Intent(activity, ProjectWarningActivity::class.java)
                startActivity(intent)
            }
            "项目统计" == operaObj.title -> {
                JniStartActivity.startIntentWebActivity(activity, H5UrlEnum.项目统计, "", "")
            }
            "单位简介" == operaObj.title -> {
                val intent = Intent(activity, UnitIntroductionActivity::class.java)
                startActivity(intent)
            }
            " 组织查询 " == operaObj.title -> {
                //街道联席会议的组织查询
                val intent = Intent(activity, StreetMeetingJianJieIntroActivity::class.java)
                startActivity(intent)
            }
            "评星晋级" == operaObj.title -> {
                val intent = Intent(activity, EvaluationOfStarActivity::class.java)
                startActivity(intent)
            }
            "政策文件" == operaObj.title -> {
                val intent = Intent(activity, PolicyDocumentActivity::class.java)
                startActivity(intent)
            }
            "红领党建" == operaObj.title -> {
                val intent = Intent(activity, RedPartyActivity::class.java)
                startActivity(intent)
            }
            "行政村管理" == operaObj.title -> {
                val intent = Intent(activity, XingZhenCunManagerActivity::class.java)
                startActivity(intent)
            }
            "四支队伍" == operaObj.title -> {
                val intent = Intent(activity, FourTeamsActivity::class.java)
                startActivity(intent)
            }
            "组织数据" == operaObj.title -> {
                JniStartActivity.startIntentWebActivity(activity, H5UrlEnum.组织数据, "", "组织数据")
            }
            "人员统计" == operaObj.title -> {
                JniStartActivity.startIntentWebActivity(activity, H5UrlEnum.党员数据, "", "人员统计")
            }
            "在线统计" == operaObj.title -> {
                JniStartActivity.startIntentWebActivity(activity, H5UrlEnum.在线统计, "", "在线统计")
            }
            "标准化数据" == operaObj.title -> {
                JniStartActivity.startIntentWebActivity(activity, H5UrlEnum.标准化数据, "", "标准化数据")

            }
        }
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?
    ) {
        super.onActivityResult(requestCode, resultCode, data)
        if (null == data) {
            return
        }
        if (requestCode == 100 && resultCode == 101) {
            val intent = Intent(activity, SheQuPartyDetailActivity::class.java)
            val orgId = data.getStringExtra(IntentJniKey.INTENT_KEY_ORGAN_ID)
            val orgName = data.getStringExtra(IntentJniKey.INTENT_KEY_ORGAN_NAME)
            intent.putExtra(IntentJniKey.INTENT_KEY_ORGAN_ID, orgId)
            intent.putExtra(IntentJniKey.INTENT_KEY_ORGAN_NAME, orgName)
            startActivity(intent)
        }
    }
}